/**
 * Copyright(c) by JNet Smartware Group.
 * All rights reserved. Not distribution or redistribution without
 * any permission of JNet Smartware Group.
 * 
 * @author thuc
 * 2010/07/31
 */
package vn.smartware.co.server.action;

import java.sql.Connection;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import vn.smartware.co.commons.Const;
import vn.smartware.co.dto.ReserveDetailDTO;
import vn.smartware.co.dto.ReserveDto;
import vn.smartware.co.forms.ReserveForm;
import vn.smartware.co.framework.action.BaseAction;
import vn.smartware.co.framework.bean.LoginDTO;
import vn.smartware.co.framework.config.SystemConfig;
import vn.smartware.co.framework.exception.ServerException;
import vn.smartware.co.framework.form.ActionForm;
import vn.smartware.co.server.dao.factory.AbstractReserveDAO;
import vn.smartware.co.server.dao.factory.DaoFactory;

/**
 * ReserveAction
 * @author thuctt
 */
@SuppressWarnings({"rawtypes"})
public class ReserveAction extends BaseAction {

	private static Log log = LogFactory.getLog(ReserveAction.class);
	
	/**
	 * get reservation info
	 * @param request
	 * @param response
	 * @param config
	 * @param aForm
	 * @return ActionForm
	 * @throws ServerException
	 */
	public ActionForm getReserveInfo(HttpServletRequest request,
			HttpServletResponse response, SystemConfig config, ActionForm aForm)
			throws ServerException {

		ReserveForm form = (ReserveForm)aForm;
		AbstractReserveDAO dao = DaoFactory.createReserveDAO();
		Connection conn = getConnection();
		
		try {
			
			LoginDTO user = (LoginDTO)form.getUserDTO();
			dao.getReserveInfo(form, user, conn);
			
			ArrayList drinkList = dao.getDetailDrink(form.getReserveDto().getReserveId(), conn);
			ArrayList foodList = dao.getDetailFood(form.getReserveDto().getReserveId(), conn);
			
			ReserveDto dto = form.getReserveDto();
			dto.setDrinkList(drinkList);
			dto.setFoodList(foodList);
			
			form.setReserveDto(dto);
						
		} catch (ServerException e) {
			log.error("ServerException.", e);
			throw e;
		} finally {
			closeConnection(conn);
		}
		return form;
	}
	
	/**
	 * register food list
	 * @param request
	 * @param response
	 * @param config
	 * @param aForm
	 * @return ActionForm
	 * @throws ServerException
	 */
	public ActionForm saveAction(HttpServletRequest request,
			HttpServletResponse response, SystemConfig config, ActionForm aForm)
			throws ServerException {

		ReserveForm form = (ReserveForm)aForm;
		LoginDTO user = (LoginDTO)form.getUserDTO();
		AbstractReserveDAO dao = DaoFactory.createReserveDAO();
		Connection conn = getConnection();
		
		try {
			ArrayList foodlist = form.getReserveDto().getFoodList();
			ArrayList drinkList = form.getReserveDto().getDrinkList();
			
			String mode = form.getProcessMode();
			if (mode.equals(Const.MODE_NEW)){//NEW
				String reserveId = dao.createReservation(form, user, conn);
				createDetail(reserveId, foodlist, ReserveForm.FOOD, user, conn);
				createDetail(reserveId, drinkList, ReserveForm.DRINK, user, conn);
				
			}else if (mode.equals(Const.MODE_EDIT)){//EDIT
				dao.updateReservation(form, user, conn);
				dao.deleteDetail(form.getReserveDto().getReserveId(), ReserveForm.FOOD, conn);
				dao.deleteDetail(form.getReserveDto().getReserveId(), ReserveForm.DRINK, conn);
				createDetail(form.getReserveDto().getReserveId(), foodlist, ReserveForm.FOOD, user, conn);
				createDetail(form.getReserveDto().getReserveId(), drinkList, ReserveForm.DRINK, user, conn);
				//Incase if bill, insert/update billing info
				if (form.getReserveDto().getReserveStatus().equals(Const.SYSTEM_CONFIG.RESERVE_STATUS_FINISHED)){
					int count = dao.chkExistBill(form.getReserveDto().getReserveId(), conn);
					if (count == 0){
						dao.createBill(form.getReserveDto().getReserveId(), form.getReserveDto().getBill(), user, conn);
					}else{
						dao.updateBill(form.getReserveDto().getReserveId(), form.getReserveDto().getBill(), user, conn);
					}
				}
				
			}else if (mode.equals(Const.MODE_DEL)) {//DELETE
				dao.deleteReservation(form, user, conn);
				dao.deleteDetail(form.getReserveDto().getReserveId(), ReserveForm.FOOD, conn);
				dao.deleteDetail(form.getReserveDto().getReserveId(), ReserveForm.DRINK, conn);
				//delete bill if exist
				int count = dao.chkExistBill(form.getReserveDto().getReserveId(), conn);
				if (count > 0){
					dao.deleteBill(form.getReserveDto().getReserveId(), user, conn);
				}
			}
			
			commit(conn);
			
		} catch (ServerException e) {
			log.error("ServerException.", e);
			rollback(conn);
			throw e;
		} finally {
			closeConnection(conn);
		}
		return form;
	}

	/**
	 * create Detail
	 * @param reserveId 
	 * @param list
	 * @param detail
	 * @param user 
	 * @param conn
	 * @throws ServerException
	 */
	private void createDetail(String reserveId, ArrayList list, int detail,
			LoginDTO user, Connection conn) throws ServerException {

		if (null == list || list.isEmpty()){
			return;
		}
		
		AbstractReserveDAO dao = DaoFactory.createReserveDAO();
		switch (detail) {
		case ReserveForm.FOOD:
			for (int i = 0; i < list.size(); i++) {
				ReserveDetailDTO food = (ReserveDetailDTO)list.get(i);
				dao.registerDetailFood(reserveId, food, user, conn);
			}
			break;
			
		case ReserveForm.DRINK:
			for (int i = 0; i < list.size(); i++) {
				ReserveDetailDTO drink = (ReserveDetailDTO)list.get(i);
				dao.registerDetailDrink(reserveId, drink, user, conn);
			}
			break;

		default:
			break;
		}
	}
	
}
